(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.settlement.paymentController", paymentController);
	paymentController.$inject = [ "$scope", "$q", "modalService", "layoutConfig", "validatorConfig", "authService", "settlementPaymentService", "settlementInitService" ];
	function paymentController($scope, $q, modalService, layoutConfig, validatorConfig, authService, settlementPaymentService, settlementInitService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {}, settlement = {};

		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			settlementPaymentService.settlementPaymentOptions().then(function(map) {
				return $q(function(resolved, rejected) {
					settlement.deliveryTypeOptions = map.deliveryTypeOptions;
					settlement.currencyOptions = map.currencyOptions;
					settlement.allPartyTypeOptions = map.partyTypeOptions;
					settlement.swiftCaseList = map.swiftCaseList;
					settlement.partyCountrys = map.countryOptionAll;
					settlement.partyStates = map.stateOptionAll;
					settlement.partyCitys = map.cityOptionAll;
					settlement.stateList = map.stateList;
					settlement.cityList = map.cityList;
					// settlement.partyCountrys = map.countryOptions;
					// settlement.partyStatesCatch = map.stateOptionAll;
					// settlement.partyCitysCatch = map.cityOptionAll;
					resolved();
				});
			}).then(function() {
				return $q(function(resolved, rejected) {
					authService.payment(params).then(function(response) {
						settlement = angular.extend(settlement, response.data.model);
						settlement.taskId = response.data.task.taskId || params.taskId;
						resolved(response);
					});
				});
			}).then(function(response){
				settlementInitService.getLayout(settlement, "payment").then(function(layout) {
					layoutConfig = layout || layoutConfig;
					angular.extend(ctrl, settlementPaymentService.getPayDetailsConfig(settlement, layoutConfig));
					ctrl.payPartyConfig = settlementPaymentService.initData(settlement, layoutConfig);
					ctrl.generalConfig = settlementInitService.getGeneralConfig(settlement.settlementInfo, layoutConfig);
					ctrl.generalModel = settlementInitService.getGeneralModel(settlement);
					ctrl.layoutConfig = layoutConfig;
					ctrl.settlement = settlement;
					displayWfInfo(response);
				});
			});
		};

		function displayWfInfo(response) {
			var messages = [];
			if (response.data.task.taskProps.wfInfo) {
				var msg = new Object();
				msg.args = response.data.task.taskProps.wfInfo.msgParams || "";
				msg.msg = response.data.task.taskProps.wfInfo.msgCode || "cls.error.unknow.exception";
				messages.push(msg);
			}
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			}
		}
		
		ctrl.custpayIdLink = function(currRow){
			modalService.openCustomDialogs({
				"title" : "popup.title.custpayview",
				"dialogsModule" : "cls.custpay.view",
				"params" : {"custpayId" : currRow.custpayId},
				"size" : [1300, 500],
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};
		
		ctrl.loanIdLink = function(currRow){
			modalService.openCustomDialogs({
				"title" : "popup.title.loanview",
				"dialogsModule" : "cls.loan.view",
				"params" : {"loanId" : currRow.loanId},
				"size" : [1300, 500],
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};
		
		ctrl.loanInfoLoanIdLink = function(loan){
			modalService.openCustomDialogs({
				"title" : "popup.title.loanview",
				"dialogsModule" : "cls.loan.view",
				"params" : {"loanId" : loan.loanInfo.loanId},
				"size" : [1300, 500],
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};
		
		ctrl.dchargeIdLink = function(currRow){
			modalService.openCustomDialogs({
				"title" : "popup.title.dchargeview",
				"dialogsModule" : "cls.dcharge.view",
				"params" : {"dchargeId" : currRow.dchargeId},
				"size" : [1300, 500],
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};
		
		ctrl.swiftTypeCallBack = function(result) {
			settlementPaymentService.swiftTypeCallBack(ctrl.settlement, layoutConfig, result);
			ctrl.payPartyConfig = settlementPaymentService.getPayPartyConfigs(ctrl.settlement, layoutConfig);
		};

		ctrl.partyCountryChanged = function(payParty) {
			settlementPaymentService.partyCountryChanged(ctrl.settlement, payParty);
		};

		ctrl.partyStateChanged = function(payParty) {
			settlementPaymentService.partyStateChanged(ctrl.settlement, payParty);
		};

		ctrl.next = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				settlementPaymentService.checkpayParties(ctrl.settlement);
				return authService.settlementPayment(ctrl.settlement).then(submitSuccessful, submitFailed);
			}
		};
		
		ctrl.back = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				modalService.openJumpDialogs({
					"messages" : [ "title.dialog.settlementpayment.back" ],
					"btnCallback" : {
						"close" : function() {
						}
					},
					"redirections" : [ {
						"name" : "title.infobar.settlement.init",
						"jump" : function() {
							settlementPaymentService.checkpayParties(ctrl.settlement);
							authService.back(ctrl.settlement).then(submitSuccessful, submitFailed);
						}
					}, {
						"name" : "title.infobar.workflow.list",
						"state" : "cls.workflow.wfTxn.list",
						"params" : {}
					} ]
				});
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

	}
})(angular);
